﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Core
{
	using System;
	using System.Threading;
	using System.Web;
	using System.Web.Mvc;
	using System.Web.Routing;
	using System.Web.Security;
	using System.Web.SessionState;
	using Falcon.Core.Authentication;

	public class Global : HttpApplication
	{
		/// <summary>
		/// Handles the Start event of the Application control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="args">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		protected void Application_Start(object sender, EventArgs args)
		{
			var application = new Application();

			// register packages
			application.RegisterPackages();

			// register container
			application.RegisterContainer();

			// register controller factory
			application.RegisterControllerFactory();

			// register view engine
			application.RegisterViewEngine(ViewEngines.Engines);

			// register routes
			application.RegisterRoutes(RouteTable.Routes);

			// initialize application
			application.Initialize();
		}

		/// <summary>
		/// Handles the End event of the Application control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="args">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		protected void Application_End(object sender, EventArgs args)
		{

		}

		/// <summary>
		/// Handles the BeginRequest event of the Application control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="args">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		protected void Application_BeginRequest(object sender, EventArgs args)
		{

		}

		/// <summary>
		/// Handles the EndRequest event of the Application control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="args">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		protected void Application_EndRequest(object sender, EventArgs args)
		{

		}

		/// <summary>
		/// Handles the OnPostAuthenticateRequest event of the Application control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="args">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		public void Application_OnPostAuthenticateRequest(object sender, EventArgs args)
		{

		}

		/// <summary>
		/// Handles the AuthenticateRequest event of the Application control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="args">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		public void Application_AuthenticateRequest(object sender, EventArgs args)
		{
			if (Context == null)
			{
				return;
			}

			var principal = Context.User;

			if (principal == null || !principal.Identity.IsAuthenticated)
			{
				var ticket = new FormsAuthenticationTicket(
					1, 
					Domain.User.Guest, 
					DateTime.Now, 
					DateTime.Now.AddMinutes(30), 
					false, 
					string.Empty
				);

				var identity = new WebUserIdentity(ticket)
				{
					IsAuthenticated = false
				};

				Context.User = new WebPermissivePrincipal(identity);

				Thread.CurrentPrincipal = Context.User;
			}
			else
			{
				var identity = principal.Identity as FormsIdentity;

				if (identity == null)
				{
					return;
				}

				var wIdentity = new WebUserIdentity(identity.Ticket)
				{
					IsAuthenticated = true
				};

				Context.User = new WebPermissivePrincipal(wIdentity);

				Thread.CurrentPrincipal = Context.User;
			}
		}

		/// <summary>
		/// Handles the PostAcquireRequestState event of the Application control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="args">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		public void Application_PostAcquireRequestState(object sender, EventArgs args)
		{
			if (Context == null || Context.CurrentHandler == null)
			{
				return;
			}

			if (typeof(IRequiresSessionState).IsAssignableFrom(Context.CurrentHandler.GetType()))
			{
				Core.Application.Session();
			}
		}
	}
}